@echo off
REM ===================================================================
REM API Test Automation Script
REM Function: Run pytest test suite, generate HTML and Allure reports
REM Author: API Automation Test Engineer
REM ===================================================================

echo ========================================
echo       API Test Automation Script
echo ========================================
echo.

REM Activate virtual environment if exists
echo [INFO] Checking virtual environment...
if exist "%~dp0..\.venv\Scripts\activate.bat" (
    echo [INFO] Activating virtual environment...
    call "%~dp0..\.venv\Scripts\activate.bat"
    echo [SUCCESS] Virtual environment activated
) else (
    echo [WARNING] Virtual environment not found, using system Python
)
echo.

REM Check Python environment
echo [INFO] Checking Python environment...
python --version >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] Python not installed or not in PATH
    echo [ERROR] Please install Python and add to PATH
    pause
    exit /b 1
)
echo [SUCCESS] Python environment check passed
echo.

REM Check if pytest is installed
echo [INFO] Checking pytest environment...
python -c "import pytest" >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] pytest not installed
    echo [INFO] Installing pytest...
    pip install pytest pytest-html allure-pytest
    if %errorlevel% neq 0 (
        echo [ERROR] pytest installation failed
        pause
        exit /b 1
    )
)
echo [SUCCESS] pytest environment check passed
echo.

REM Change to project root directory (APItest)
cd /d "%~dp0.."
set PROJECT_ROOT=%cd%
echo [INFO] Working directory: %PROJECT_ROOT%

REM Create report directories
echo [INFO] Creating report directories...
if not exist "reports" mkdir reports
if not exist "reports\allure-results" mkdir reports\allure-results
echo [SUCCESS] Report directories created
echo.

REM Clean old test results
echo [INFO] Cleaning old test results...
if exist "reports\*.html" del /q "reports\*.html"
if exist "reports\*.xml" del /q "reports\*.xml"
if exist "reports\allure-results\*" del /q "reports\allure-results\*"
echo [SUCCESS] Old test results cleaned
echo.

REM Run tests
echo ========================================
echo           Starting API Tests
echo ========================================
echo.
echo [INFO] Running pytest test suite...
echo [INFO] Test reports will be generated in reports directory
echo [INFO] Allure results will be saved in reports/allure-results directory
echo [INFO] Performance tests will be excluded from this run
echo.

REM Execute pytest command (exclude performance tests)
pytest tests/ --html=reports/report.html --self-contained-html --junitxml=reports/junit-report.xml --alluredir=reports/allure-results --tb=short -v --ignore=tests/performance/

REM Check test results
if %errorlevel% equ 0 (
    echo.
    echo ========================================
    echo           Test Execution Complete
    echo ========================================
    echo [SUCCESS] All tests passed!
) else (
    echo.
    echo ========================================
    echo           Test Execution Complete
    echo ========================================
    echo [WARNING] Some tests failed, please check report details
)

echo.
echo [INFO] Test reports generated:
echo [INFO] - HTML report: reports\report.html
echo [INFO] - JUnit report: reports\junit-report.xml
echo [INFO] - Allure results: reports\allure-results\
echo.

REM Generate Allure report
echo [INFO] Generating Allure report...
allure generate reports\allure-results -o reports\allure-report --clean
if %errorlevel% equ 0 (
    echo [SUCCESS] Allure report generated: reports\allure-report\index.html
    echo [INFO] Opening Allure report...
    start reports\allure-report\index.html
) else (
    echo [WARNING] Allure report generation failed, please check if allure is installed
    echo [INFO] Install command: npm install -g allure-commandline
)

echo.
echo ========================================
echo           Script Execution Complete
echo ========================================
echo [INFO] Thank you for using API Test Automation Script!
echo.
pause